//
//  ZuoJMainController.swift
//  Tanenbaum_blog
//
//  Created by Tanenbaum on 15/7/28.
//  Copyright © 2015年 GZJ. All rights reserved.
//

import UIKit

class ZuoJMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        addChildViewControllers()
        
       
    }
    
    override func viewDidAppear(animated: Bool) {
         super.viewDidAppear(animated)
        
          setUpcomposeButton()
    }
    
    //设置按钮的frame
    private func setUpcomposeButton() {
        
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        
        composeButton.frame = CGRectOffset(rect, w * 2, 0)
        
    }
    
    private func addChildViewControllers() {
        
        addChildViewController(ZuoJHomeController(), title: "首页", image: "tabbar_home")
        addChildViewController(ZuoJMessageController(), title: "消息", image: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(ZuoJDiscoverController(), title: "发现", image: "tabbar_discover")
        addChildViewController(ZuoJProfileController(), title: "我", image: "tabbar_profile")
    }
    
    
    private func addChildViewController(vc: UIViewController ,title: String,  image:String) {
        
//        tabBar.tintColor = UIColor.orangeColor()
        
        vc.tabBarItem.image = UIImage(named: image)
        
        vc.title = title;
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    func ClickComposeButton() {
        
        let vc = UserAccount.userLogon ? ZuoJComposeViewController() : OAuthViewController()

        
        let nav = UINavigationController(rootViewController: vc)
        presentViewController(nav, animated: true, completion: nil)
    }
    
    lazy private var composeButton: UIButton = {
        
        let button = UIButton()
        
         button.setImage(UIImage(named:"tabbar_compose_icon_add" ), forState: UIControlState.Normal)
         button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "ClickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
        self.tabBar.addSubview(button)
        
        return button
    }()

}
